import { DynamicModule, Global, Module } from '@nestjs/common';
import { QiniuModuleAsyncOptions } from './qiniu.interface';

import { QiniuService } from './qiniu.service';
import {
  createAsyncQiniuOptions,
  createQiniuOptions,
  QiniuOptions,
} from './qiniu-options.provider';
import { QINIU_MODULE_OPTIONS } from './qiniu.constants';

@Global()
@Module({
  providers: [QiniuService],
  // exports: [QiniuService],
})
export class QiniuCoreModule {
  static register(options: QiniuOptions): DynamicModule {
    return {
      module: QiniuCoreModule,
      providers: [
        createQiniuOptions(),
        { provide: QINIU_MODULE_OPTIONS, useValue: options },
      ],
      exports: [QiniuService],
    };
  }

  static forRootAsync(options: QiniuModuleAsyncOptions): DynamicModule {
    return {
      module: QiniuCoreModule,
      imports: options.imports,
      providers: [createQiniuOptions(), createAsyncQiniuOptions(options)],
      exports: [QiniuService],
    };
  }
}
